package com.regentsoft.cloudback.operateAdmin.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.regentsoft.cloudback.base.Pager;
import com.regentsoft.cloudback.operateAdmin.dao.ApplicationMapper;
import com.regentsoft.cloudback.operateAdmin.dao.CompanyApplicationMapper;
import com.regentsoft.cloudback.operateAdmin.dao.CompanyMapper;
import com.regentsoft.cloudback.operateAdmin.entity.Application;
import com.regentsoft.cloudback.operateAdmin.entity.Company;
import com.regentsoft.cloudback.operateAdmin.entity.CompanyApplication;
import com.regentsoft.cloudback.operateAdmin.service.ApplicationService;
import com.regentsoft.cloudback.util.DateUtil;
import com.regentsoft.cloudback.util.HttpClientUtil;
import com.regentsoft.cloudback.util.PropertyUtil;


/**
 * 
 * @author czd
 *
 * @date 2017年11月25日
 *
 * @version 1.0
 */
@Service
public class ApplicationServiceImpl implements ApplicationService {
	@Autowired
	private ApplicationMapper mapper;
	@Autowired
	private CompanyMapper companyMapper;
	@Autowired
	private CompanyApplicationMapper companyApplicationMapper;
	
	
	@Override
	public Application findById(String pk) throws Exception {
		return mapper.findById(pk);
	}

	@Override
	public List<Application> findList(Application obj) throws Exception {
		return mapper.findList(obj);
	}

	@Override
	public Pager findPageList(Application obj, Integer pageNumber, Integer pageSize) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int add(Application obj) throws Exception {
		return mapper.add(obj);
	}

	@Override
	public int update(Application obj) throws Exception {
		return mapper.update(obj);
	}

	@Override
	public int delete(String pk) throws Exception {
		return mapper.deleteById(pk);
	}

	@Override
	public int getTotalCount(Map<String, Object> map) throws Exception {
		return mapper.getTotalCount(map);
	}

	@Override
	public List<Application> queryByPage(Map<String, Object> map) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Application selectByCode(String code) {
		return mapper.selectByCode(code);
	}

	@Override
	public String findByCode(String code) {
		return mapper.findByCode(code);
	}

	@Override
	public void addOrEditApplication(String code, String link, String typeId,String account,String password) throws Exception {
		// 根据code查找企业信息
		try {
			Company company = companyMapper.findCompanyByCode(code);
			if(company==null) {
				return;
			}
			CompanyApplication oldCompanyApplication = companyApplicationMapper.findByCompanyId(company.getId());
			if(oldCompanyApplication!=null) {
				oldCompanyApplication.setAccount(account);
				oldCompanyApplication.setPassword(password);
				oldCompanyApplication.setAppId(typeId);
				oldCompanyApplication.setCompanyId(company.getId());
				oldCompanyApplication.setLink(link);
				companyApplicationMapper.update(oldCompanyApplication);
			}else {
				//根据appId查找appCode
				Application application= mapper.findById(typeId);
				if(application==null) {
					return;
				}
				CompanyApplication CompanyApplication = new CompanyApplication();
				CompanyApplication.setId(UUID.randomUUID().toString());
				CompanyApplication.setAccount(account);
				CompanyApplication.setPassword(password);
				CompanyApplication.setAppId(typeId);
				CompanyApplication.setCompanyId(company.getId());
				CompanyApplication.setLink(link);
				companyApplicationMapper.add(CompanyApplication);
				String currentTime = DateUtil.timeFormat(new Date());
				String[] arr = currentTime.split(" ");
				currentTime = arr[0]+"%20"+arr[1];
				companyApplicationMapper.initConfigData(code,currentTime);
				// 向店小二发送同步消息
				String resultCode = initShopCompany(code,application.getCode());
				if(resultCode.equals("0")) {
					return;
				}else{
					throw new RuntimeException("向店小二发送同步消息失败",null);
				}
				
			}
				
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(),e);
		}
		
	}
	/**
	 *  向店小二发送同步消息
	 * @param code
	 * @param appCode
	 * @return
	 */
	private String initShopCompany(String code,String appCode) {
		String url = PropertyUtil.getProperty("SYSTEM_SC");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", code);
		map.put("appCode", appCode);
		String params = HttpClientUtil.getUrlParamsByMap(map);
		String result = HttpClientUtil.sendPost(url, params);
		if(StringUtils.isEmpty(result)) {
			System.out.println("失败");
			return "-1";
		}
		Map<String,Object> resultMap = HttpClientUtil.toMap(result);
		if(resultMap!=null) {
			return (String) resultMap.get("code");
		}else {
			return "-1";
		}
		
	}

	
	
}
